<template>
    <div>
        <el-table
            ref="multipleTable"
            style="width: 100%"
            tooltip-effect="dark"
            :data="projectPlans"
        >
            <!-- <el-table-column
                align="left"
                label="序号"
                prop="serial_num"
                width="60"
            /> -->
            <el-table-column
                align="left"
                label="节点"
                prop="operate_node"
                min-width="120"
            />
            <el-table-column
                align="left"
                label="默认周期"
                prop="life_cycle"
                min-width="80"
            >
            </el-table-column>
            <!-- <el-table-column
                prop="planningLifeCycle"
                align="left"
                label="计划周期"
                min-width="100"
            >
                <template #default="scope">
                    <el-input-number
                        v-model="scope.row.planningLifeCycle"
                        @change="dataNumChange(scope.row)"
                        :min="0"
                        style="width: 100%"
                        controls-position="right"
                        :disabled="props.isEditing"
                    ></el-input-number>
                </template>
            </el-table-column> -->
            <!-- <el-table-column align="left" label="关联节点" min-width="150">
                <template #default="{ row, $index }">
                    <template v-if="props.isEditing">
                        <el-select
                            v-model="row.association_node_id"
                            v-if="$index != 0"
                            style="width: 95%"
                            :clearable="true"
                            filterable
                            disabled
                        >
                            <el-option
                                v-for="(item, i) in projectPlans"
                                :key="i"
                                :label="item.operate_node"
                                :value="item.node_id"
                            />
                        </el-select>
                    </template>
                    <template v-else>
                        <el-select
                            v-model="row.association_node_id"
                            v-if="$index != 0"
                            style="width: 95%"
                            :clearable="true"
                            filterable
                            :disabled="
                                ['2D/3D绘图', '建立产品规格', '起模'].includes(
                                    row.operate_node
                                )
                            "
                            @change="associationOperateNodeChange"
                        >
                            <el-option
                                v-for="(item, i) in projectPlans"
                                :key="i"
                                :label="item.operate_node"
                                :value="item.node_id"
                            />
                        </el-select>
                    </template>
                </template>
            </el-table-column> -->
            <el-table-column
                prop="planned_start_date"
                align="left"
                label="计划开始时间"
                min-width="120"
            >
                <template #default="scope">
                    {{ scope.row.planned_start_date }}
                </template>
            </el-table-column>
            <el-table-column
                prop="planned_completion_date"
                align="left"
                label="计划结束时间"
                min-width="120"
            >
                <template #default="scope">
                    {{ scope.row.planned_completion_date }}
                </template>
            </el-table-column>
            <el-table-column align="left" label="实际开始时间" min-width="120">
                <template #default="scope">{{
                    formatDate(scope.row.actual_start_cycle)
                }}</template>
            </el-table-column>
            <el-table-column align="left" label="实际结束时间" min-width="100">
                <template #default="scope">{{
                    formatDate(scope.row.actual_finishing_date)
                }}</template>
            </el-table-column>
        </el-table>
    </div>
</template>

<script setup>
import {
    ref,
    reactive,
    onMounted,
    computed,
    defineExpose,
    defineProps,
} from "vue";
import { generateProductStylesPlan } from "@/api/plmProductStyles";
import { formatDate } from "@/utils/format";
import { deepClone2, debounce } from "@/utils/tool.js";

const props = defineProps({
    // projectPlans: {
    //     type: Array,
    //     default: [],
    // },
    formData: {
        type: Object,
        default: {},
    },
    isEditing: {
        type: Boolean,
        default: false,
    },
});

const projectPlans = ref([]);

// 查询计划时间
const generateProductStylesPlanFun = async () => {
    projectPlans.value = [];
    if (!props.formData.establishOrder || !props.formData.drawer) {
        return;
    }
    let params = {
        establishOrder: props.formData.establishOrder,
        drawer: props.formData.drawer,
    };
    const table = await generateProductStylesPlan(params);
    if (table.code === 0) {
        projectPlans.value = table.data.list.map((item) => {
            item["planned_start_date"] = formatDate(item.planned_start_date);
            item["planned_completion_date"] = formatDate(
                item.planned_completion_date
            );
            return item;
        });
        associationOperateNodeChange();
    }
};

// 开办顺序改变
const establishOrderChange = () => {
    generateProductStylesPlanFun();
};
// 绘图改变
const drawerChange = () => {
    generateProductStylesPlanFun();
};
// 关联节点改变
const associationOperateNodeChange = () => {
    dataNumChange();
};

const sumDay = (dateStr, days) => {
    const date = new Date(dateStr);
    date.setDate(date.getDate() + days - 1);
    return formatDate(date.toISOString());
};

// 时间改变plannedStartDate,
const dataNumChange = () => {
    for (let i = 0; i < projectPlans.value.length; i++) {
        let item = projectPlans.value[i];
        if (item.node_id == 1) {
            let data1 = projectPlans.value[0].planned_start_date;
            let data2 = sumDay(data1, +item.planningLifeCycle);
            item["planned_completion_date"] = data2;
        } else if (
            item.association_node_id &&
            item.node_id != item.association_node_id
        ) {
            let i = projectPlans.value.findIndex((d) => {
                return item.association_node_id == d.node_id;
            });
            let data1 = sumDay(
                projectPlans.value[i].planned_completion_date,
                2
            );
            item["planned_start_date"] = data1;
            let data2 = sumDay(data1, +item.planningLifeCycle);
            item["planned_completion_date"] = data2;
        } else {
            item["planned_start_date"] = item.planned_start_date;
            let data2 = sumDay(item.planned_start_date, item.planningLifeCycle);
            item["planned_completion_date"] = data2;
        }
    }
    // for (let i = projectPlans.value.length - 1; i > 0; i--) {
    //     let item = projectPlans.value[i];
    //     if (item.node_id == 1) {
    //         let data1 = projectPlans.value[0].planned_start_date;
    //         let data2 = sumDay(data1, +item.serial_num);
    //         item["planned_completion_date"] = data2;
    //     } else if (item.node_id != item.association_node_id) {
    //         let i = projectPlans.value.findIndex((d) => {
    //             return item.association_node_id == d.node_id;
    //         });
    //         let data1 = sumDay(
    //             projectPlans.value[i].planned_completion_date,
    //             1
    //         );
    //         item["planned_start_date"] = data1;
    //         let data2 = sumDay(data1, +item.serial_num);
    //         item["planned_completion_date"] = data2;
    //     }
    // }
};

// 两个日期间隔

function daysBetween(date1, date2) {
    const oneDay = 24 * 60 * 60 * 1000; // 每天的毫秒数
    //   const firstDate = new Date(date1);
    //   const secondDate = new Date(date2);
    //   const diff = secondDate - firstDate; // 计算两日期差值
    const diff = date1 - date2; // 计算两日期差值
    return Math.floor(diff / oneDay); // 将差值转换为天数并向下取整
}
// 合计
const getTotal = (param) => {
    const { columns, data } = param;
    const sums = [];
    columns.forEach((column, index) => {
        if (index === 0) {
            sums[index] = "合计";
            return;
        }
        const values = data.map((item) => Number(item[column.property]));
        switch (column.property) {
            // case "life_cycle":
            //     sums[index] = values.reduce((prev, curr) => {
            //         const value = Number(curr);
            //         if (!isNaN(value)) {
            //             return prev + curr;
            //         } else {
            //             return prev;
            //         }
            //     }, 0);
            //     sums[index];
            //     break;
            // case "planningLifeCycle":
            //     sums[index] = values.reduce((prev, curr) => {
            //         const value = Number(curr);
            //         if (!isNaN(value)) {
            //             return prev + curr;
            //         } else {
            //             return prev;
            //         }
            //     }, 0);
            //     console.log("maxDate", values);
            //     sums[index];
            //     break;
            // case "planned_start_date":
            //     let dateL = data.map((d) => {
            //         return d.planned_start_date;
            //     });
            //     // 找到最大日期
            //     var maxDate = new Date(
            //         Math.max.apply(
            //             null,
            //             dateL.map(function (date) {
            //                 return Date.parse(date);
            //             })
            //         )
            //     );
            //     // 找到最小日期
            //     var minDate = new Date(
            //         Math.min.apply(
            //             null,
            //             dateL.map(function (date) {
            //                 return Date.parse(date);
            //             })
            //         )
            //     );

            //     // console.log("dateL", dateL);
            //     // console.log("maxDate", maxDate);
            //     // console.log("minDate", minDate);
            //     // console.log("daysBetween", daysBetween(maxDate, minDate));
            //     sums[index] = daysBetween(maxDate, minDate);
            //     break;
            case "planned_completion_date":
                let dateL1 = data.map((d) => {
                    return d.planned_start_date;
                });
                // 找到最小日期
                var minDate = new Date(
                    Math.min.apply(
                        null,
                        dateL1.map(function (date) {
                            return Date.parse(date);
                        })
                    )
                );
                let dateL2 = data.map((d) => {
                    return d.planned_completion_date;
                });
                // 找到最大日期
                var maxDate = new Date(
                    Math.max.apply(
                        null,
                        dateL2.map(function (date) {
                            return Date.parse(date);
                        })
                    )
                );
                // console.log("maxDate",values)
                // sums[index] = values.reduce((prev, curr) => {
                //     const value = Number(curr);
                //     if (!isNaN(value)) {
                //         return prev + curr;
                //     } else {
                //         return prev;
                //     }
                // }, 0);
                sums[index] = daysBetween(maxDate, minDate);
                break;
            default:
                sums[index] = "---";
        }
    });

    return sums;
};

const updateValue = (val) => {
    projectPlans.value = deepClone2(
        val.map((item) => {
            item["planned_start_date"] = formatDate(item.planned_start_date);
            item["planned_completion_date"] = formatDate(
                item.planned_completion_date
            );
            return item;
        })
    );
};

defineExpose({ establishOrderChange, drawerChange, updateValue, projectPlans });
</script>

<style lang="scss" scoped>
</style>